#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import argparse
import ConfigParser
import feedparser
import os
import re
import sys
from daemon import DaemonContext
from daemon.pidlockfile import PIDLockFile
from textwrap import TextWrapper

from NetEbbms import NetEbbms

import psyco
psyco.full()

def main():
	# スクリプトパスを取得 #
	scriptDirPath = os.path.abspath(os.path.dirname(__file__))


	# 設定ファイルディレクトリパスを取得 #
	configDirPath = os.path.join(os.path.split(os.path.split(scriptDirPath)[0])[0], 'config')


	# 設定ファイルパスを取得 #
	configFilePath = os.path.join(configDirPath, 'netebbms.conf')


	# モデル リスト ファイル パスを取得 #
	modelListFilePath = os.path.join(configDirPath, 'model.list')


	# インスタンス作成 #
	myNetEbbms = NetEbbms(configFilePath, modelListFilePath)


	# 設定ファイル読み込み #
	config = ConfigParser.SafeConfigParser()
	config.read(os.path.join(scriptDirPath, 'rssreader.conf'))
	

	# 
	config_siteUrl = config.get('site', 'url')
	config_styleCommon = config.get('style_common', 'style_common')

	config_styleTitleFontpath = config.get('style_title', 'fontpath')


if __name__ == "__main__":
	# コマンドラインオプションをパース #
	parser = argparse.ArgumentParser()
	parser.add_argument(
		'-d', '--daemon',
		action = 'store_true',
		default = False,
		dest = 'runDaemon',
		help = 'run this script as daemon'
	)
	args = parser.parse_args()

	try:
		if args.runDaemon == True:
			# デーモンとして起動 #
			dc = DaemonContext(
				pidfile = PIDLockFile('/var/run/netebbms/rssreader.pid'),
				stderr = open('/var/log/netebbms/rssreader_error.log', 'w+')
			)
	
			with dc:
				main()

		else:
			# コマンドとして起動 #
			main()

	except KeyboardInterrupt:
		pass

	except Exception, ex:
		print >> sys.stderr, ex
		sys.exit(1)




def sample_main():
	# RSSを取得 #
	try:
		while True:
			for url in urlList:
				fdp = feedparser.parse(url)

				print '<<<<<<<<<<' + fdp.channel.title + '>>>>>>>>>>'

				for entry in fdp.entries:
					if 'title' in entry:
						print entry.title

					if 'link' in entry:
						print entry.link

					if 'updated_parsed' in entry:
						print entry.updated_parsed

					if 'summary' in entry:
						print entry.summary


					self.netebbms.setText(
						generator = 'rss reader',
						memo = '[' + status.author.screen_name.encode('utf-8') + '  ' + status.author.name.encode('utf-8'    ) + ']\n' +
						status.source.encode('utf-8') + '\n' + status.text.encode('utf-8'),
						mode = 'COUNT',
						enable = True,
						count = 1,
						end_time = 0.0,
						backcolor = self.config.get('style_common', 'backcolor'),
						textstyle = (
							{
								'text' : status.text.encode('utf-8'),
								'fontpath' : self.config.get('style_tweettext', 'fontpath'),
								'color' : self.config.get('style_tweettext', 'color'),
								'point' : self.config.getint('style_tweettext', 'point'),
								'scroll' : self.config.getint('style_tweettext', 'scroll'),
								'positionY' : self.config.getint('style_tweettext', 'positionY')
							},
							{
								'text' : status.author.screen_name.encode('utf-8'),
								'fontpath' : self.config.get('style_author', 'fontpath'),
								'color' : self.config.get('style_author', 'color'),
								'point' : self.config.getint('style_author', 'point'),
								'scroll' : self.config.getint('style_author', 'scroll'),
								'positionX' : self.config.getint('style_author', 'positionX'),
								'positionY' : self.config.getint('style_author', 'positionY')
							}
						)
					)

			break


	except Exception:
		myNetEbbms.close()


if __name__ == "__main__":
	try:
		main()

	except KeyboardInterrupt:
		pass

	except Exception, ex:
		print >> sys.stderr, ex
		sys.exit(1)




#					self.netebbms.setText(
#						generator = 'twitter',
#						memo = '[' + status.author.screen_name.encode('utf-8') + '  ' + status.author.name.encode('utf-8') + ']\n' +
#							status.source.encode('utf-8') + '\n' + status.text.encode('utf-8'),
#						mode = 'COUNT',
#						enable = True,
#						count = 1,
#						end_time = 0.0,
#						backcolor = self.config.get('style_common', 'backcolor'),
#						textstyle = (
#							{
#								'text' : status.text.encode('utf-8'),
#								'fontpath' : self.config.get('style_tweettext', 'fontpath'),
#								'color' : self.config.get('style_tweettext', 'color'),
#								'point' : self.config.getint('style_tweettext', 'point'),
#								'scroll' : self.config.getint('style_tweettext', 'scroll'),
#								'positionY' : self.config.getint('style_tweettext', 'positionY')
#							},
#							{
#								'text' : status.author.screen_name.encode('utf-8'),
#								'fontpath' : self.config.get('style_author', 'fontpath'),
#								'color' : self.config.get('style_author', 'color'),
#								'point' : self.config.getint('style_author', 'point'),
#								'scroll' : self.config.getint('style_author', 'scroll'),
#								'positionX' : self.config.getint('style_author', 'positionX'),
#								'positionY' : self.config.getint('style_author', 'positionY')
#							}
#						)
#					)
#
